﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class StockTakeBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public StockTakeBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string SerialNo)
        {
            if (DAO.T_STOCKTAKEs.Where(t => t.SERIAL_NO == Convert.ToInt32(SerialNo)).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_STOCKTAKE> GetStockTakeList()
        {
            var list = DAO.T_STOCKTAKEs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }


            return list;
        }

        public T_STOCKTAKE GetStockTakeByStockTakeID(string StockTakeID)
        {
            return DAO.T_STOCKTAKEs.Where(t => t.STOCKTAKE_ID.ToString() == StockTakeID).SingleOrDefault();
        }
        public IEnumerable<T_STOCKTAKE_DETAIL> GetStockTakeDetailByStockTakeID(string StockTakeID)
        {
            var list = DAO.T_STOCKTAKE_DETAILs.Where(t => t.STOCKTAKE_ID.ToString() == StockTakeID);
            if (!string.IsNullOrEmpty(OrderCondition.Trim()))
            {
                list = list.OrderBy(OrderCondition);
            }
            return list.ToList() ;
        }
        public int AddStockTake(T_STOCKTAKE StockTake)
        {
            try
            {
                StockTake.STOCKTAKE_ID = DAO.GetGUID();

                DAO.T_STOCKTAKEs.InsertOnSubmit(StockTake);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddStockTake(T_STOCKTAKE StockTake, out string StockTakeGuid)
        {
            try
            {
                StockTake.STOCKTAKE_ID = DAO.GetGUID();
                
                DAO.T_STOCKTAKEs.InsertOnSubmit(StockTake);
                DAO.SubmitChanges();

                StockTakeGuid = StockTake.STOCKTAKE_ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                StockTakeGuid = "";
                return -1;
            }
        }

        public int UpdateStockTake(T_STOCKTAKE StockTake)
        {
            try
            {
                T_STOCKTAKE updateEntity = DAO.T_STOCKTAKEs.SingleOrDefault(t=>t.STOCKTAKE_ID.ToString()==StockTake.STOCKTAKE_ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.AUDIT_TIME = StockTake.AUDIT_TIME;
                updateEntity.AUDITOR_ID = StockTake.AUDITOR_ID;
                updateEntity.CREATE_TIME = StockTake.CREATE_TIME;
                updateEntity.CREATOR_ID = StockTake.CREATOR_ID;
                updateEntity.END_TIME = StockTake.END_TIME;
                updateEntity.REMARK = StockTake.REMARK;
                updateEntity.START_TIME = StockTake.START_TIME;
                updateEntity.STATE = StockTake.STATE;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteStockTake(string StockTakeID)
        {
            try
            {
                T_STOCKTAKE deleteEntity = DAO.T_STOCKTAKEs.SingleOrDefault(t => t.STOCKTAKE_ID.ToString() == StockTakeID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_STOCKTAKE_DETAILs.DeleteAllOnSubmit(deleteEntity.T_STOCKTAKE_DETAILs);
                DAO.T_STOCKTAKEs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteStockTakes(List<string> StockTakeIDs)
        {
            try
            {
                foreach (string StockTakeID in StockTakeIDs)
                {
                    T_STOCKTAKE deleteEntity = DAO.T_STOCKTAKEs.SingleOrDefault(t => t.STOCKTAKE_ID.ToString() == StockTakeID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_STOCKTAKE_DETAILs.DeleteAllOnSubmit(deleteEntity.T_STOCKTAKE_DETAILs);
                    DAO.T_STOCKTAKEs.DeleteOnSubmit(deleteEntity);
                }
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public bool IsAudited(string ID)
        {
            T_STOCKTAKE entity = DAO.T_STOCKTAKEs.SingleOrDefault(t => t.STOCKTAKE_ID.ToString() == ID);

            return entity.STATE == (int)Enumerations.StockStatus.审核通过 ? true : false;
        }
    }
}
